<template>
  <div class="app-container calendar-list-container">
    <el-form :model="form" ref="form" :rules="rules" label-width="100px">
      <el-form-item label="分组图标">
        <el-upload
          class="avatar-uploader"
          name="imageFile"
          :headers="requestHeader"
          :action="uploadImgUrl"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
          :data="uploadParam"
        >
          <img v-if="form.groupImage" :src="imgBaseURL + form.groupImage" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          <div class="el-upload__tip" slot="tip">只能上传jpg/png文件，且不超过2MB</div>
        </el-upload>
      </el-form-item>
      <el-form-item label="分组名称*" prop="groupName">
        <el-input v-model="form.groupName" placeholder="请输入分组名称"></el-input>
      </el-form-item>

      <el-form-item label="关联店铺*">
        <el-select placeholder="请输入店铺名称或下拉选择" clearable filterable v-model="form.shopId">
          <el-option
            v-for="item in  region"
            :key="item.shopId"
            :label="item.shopName"
            :value="item.shopId"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="状态*">
        <template>
          <el-radio v-model="form.groupStatus" label="1">有效</el-radio>
          <el-radio v-model="form.groupStatus" label="0">无效</el-radio>
        </template>
      </el-form-item>
      <div class="dialog-footer">
        <el-button @click="closeShopShops" id="shopJuLiS">取 消</el-button>
        <el-button type="primary" @click="cateGroup('form')" id="shopsJuLi">确 定</el-button>
      </div>
    </el-form>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import { getToken } from "utils/auth";
import { getAllShopList } from "api/shop/shops/index";
import { addGoodsGroup } from "api/shop/goods/group/index";
export default {
  name: "addGroupManage",
  data() {
    return {
      imgBaseURL: this.GLOBAL.imgBaseURL,
      requestHeader: { token: getToken() },
      uploadImgUrl: this.GLOBAL.uploadImgUrl,
      uploadParam: { useType: "group" },
      region: [],
      form: {
        groupId: undefined,
        groupName: undefined,
        groupStatus: undefined,
        groupImage: undefined,
        shopId: undefined
      },
      rules: {
        groupName: [
          {
            required: true,
            message: "请输入商品名称",
            trigger: "blur"
          }
        ]
      }
    };
  },
  created() {
    this.getAllShopList();
  },
  computed: {
    ...mapGetters(["elements"])
  },
  methods: {
    getAllShopList() {
      getAllShopList().then(response => {
        this.region = response.data;
      });
    },
    handleAvatarSuccess(res, file) {
      this.form.groupImage = res.data;
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isPNG = file.type === "image/png";
      const isLt2M = file.size /1024/1024 < 2;;

      if (!isJPG && !isPNG) {
        this.$message.error("上传图片只能是 JPG/PNG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传图片大小不能超过 2MB!");
      }
      return (isJPG || isPNG) && isLt2M;
    },
    closeShopShops() {
      this.$router.push("/goodManager/goodsGroupManager");
    },
    cateGroup(form) {
      if (this.form.groupImage == "" || this.form.groupImage == null) {
        this.$message.error("分组图标不能为空");
        return false;
      }
      if (this.form.shopId == "" || this.form.shopId == null) {
        this.$message.error("关联店铺不能为空");
        return false;
      }
      if (this.form.groupStatus == "" || this.form.groupStatus == null) {
        this.$message.error("状态不能为空");
        return false;
      }
      const set = this.$refs;
      set[form].validate(valid => {
        if (valid) {
          addGoodsGroup(this.form).then(response => {
            if (response.message == 200) {
              this.$notify({
                title: "成功",
                message: "添加成功!",
                type: "success",
                duration: 2000
              });
              this.$router.push("/goodManager/goodsGroupManager");
            } else {
              this.$notify({
                title: "失败",
                message: "数据出现异常!",
                type: "error",
                duration: 2000
              });
              this.$router.push("/goodManager/goodsGroupManager");
            }
          });
        }
      });
    }
  }
};
</script>
<style>
#shopJuLiS {
  margin-left: 70px;
}
#shopsJuLi {
  margin-left: 350px;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 100px;
  height: 100px;
  line-height: 100px;
  text-align: center;
}
.avatar {
  width: 100px;
  height: 100px;
  display: block;
}
</style>
